package com.appkubes.mall.admin.application.user.account.account.dao;

import com.appkubes.mall.admin.application.user.account.account.entity.dto.AccountResultDTO;
import com.appkubes.mall.admin.application.user.account.account.entity.dto.AccountStatisDTO;
import com.appkubes.mall.admin.application.user.account.account.entity.vo.AccountSearchVO;
import com.appkubes.mall.core.dao.BaseMallUserAccountDao;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

@Mapper
@Repository
public interface UserAccountDao extends BaseMallUserAccountDao {

    @Select({"<script>",
            " SELECT `user`.name, `user`.tel, `account`.total_brokerage,",
            " mp.promoter_key, mp.promoter_name",
            " FROM mall_user_account as `account`",
            " LEFT JOIN mall_user as `user` ON `account`.user_key=`user`.user_key",
            " LEFT JOIN mall_user_hierarchy as  muh  ON `muh`.user_key=`account`.user_key", //账户对应的层级关系
            " LEFT JOIN mall_promoter as  mp  ON `mp`.promoter_key=`muh`.promoter_key", //推广员信息(渠道)
            " WHERE `user`.tel is not NULL",
            " <when test='accountSearchVO.name!=null'>",
                    " AND `user`.name like '%${accountSearchVO.name}%'",
                    " </when>",
            " <when test='accountSearchVO.tel!=null'>",
                    " AND `user`.tel like '%${accountSearchVO.tel}%'",
                    " </when>",
            " <when test='accountSearchVO.promoterKey!=null'>" +
                    " AND `muh`.promoterKey like '%${accountSearchVO.promoterKey}%'" +
                    " </when>",
            "</script>"})
    IPage<AccountResultDTO> search(IPage<AccountResultDTO> page, @Param("accountSearchVO")AccountSearchVO accountSearchVO);


    @Select({"<script>",
            " SELECT SUM(`account`.total_brokerage) as brokerage",
            " FROM mall_user_account as `account`",
            " LEFT JOIN mall_user as `user` ON `account`.user_key=`user`.user_key",
            " LEFT JOIN mall_user_hierarchy as  muh  ON `muh`.user_key=`account`.user_key", //账户对应的层级关系
            " LEFT JOIN mall_promoter as  mp  ON `mp`.promoter_key=`muh`.promoter_key", //推广员信息(渠道)
            " WHERE `user`.tel is not NULL",
            " <when test='accountSearchVO.name!=null'>",
            " AND `user`.name like '%${accountSearchVO.name}%'",
            " </when>",
            " <when test='accountSearchVO.tel!=null'>",
            " AND `user`.tel like '%${accountSearchVO.tel}%'",
            " </when>",
            " <when test='accountSearchVO.promoterKey!=null'>" +
                    " AND `muh`.promoterKey like '%${accountSearchVO.promoterKey}%'" +
                    " </when>",
            "</script>"})
    AccountStatisDTO statis(@Param("accountSearchVO")AccountSearchVO accountSearchVO);
}
